package com.ruoyi.cderp.service.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.ruoyi.cderp.domain.MaterialStock;
import com.ruoyi.cderp.domain.ProductionOrder;
import com.ruoyi.cderp.mapper.MaterialStockMapper;
import com.ruoyi.cderp.mapper.ProductionOrderMapper;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Random;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.cderp.domain.ProductionPickingItem;
import com.ruoyi.cderp.mapper.ProductionPickingMapper;
import com.ruoyi.cderp.domain.ProductionPicking;
import com.ruoyi.cderp.service.IProductionPickingService;

/**
 * 生产领料Service业务层处理
 *
 * @author wzy
 * @date 2025-06-11
 */
@Service
public class ProductionPickingServiceImpl implements IProductionPickingService
{
    //引入生产领料Mapper
    @Autowired
    private ProductionPickingMapper productionPickingMapper;

    //引入库存Mapper
    @Autowired
    private MaterialStockMapper  materialStockMapper;

    //引入生产工单Mapper
    @Autowired
    private ProductionOrderMapper productionOrderMapper;



    @Override
    public List<ProductionPickingItem> selectOrderPickingList(String id) {
        List<ProductionPickingItem> productionPickingItemList = productionPickingMapper.selectOrderPickingList(id);
        for(ProductionPickingItem productionPickingItem:productionPickingItemList){
            //有小数的强制向上取整
            productionPickingItem.setActualQuantity(productionPickingItem.getActualQuantity().setScale(0, RoundingMode.CEILING));
        }
        return productionPickingItemList;
    }


    /**
     * 查询生产领料
     *
     * @param id 生产领料主键
     * @return 生产领料
     */
    @Override
    public ProductionPicking selectProductionPickingById(String id)
    {
        ProductionPicking productionPicking = productionPickingMapper.selectProductionPickingById(id);
        productionPicking.setProductionPickingItemList(productionPickingMapper.selectProductionPickingItemById(id));
        return productionPicking;
    }

    /**
     * 查询生产领料列表
     *
     * @param productionPicking 生产领料
     * @return 生产领料
     */
    @Override
    public List<ProductionPicking> selectProductionPickingList(ProductionPicking productionPicking)
    {
        return productionPickingMapper.selectProductionPickingList(productionPicking);
    }

    /**
     * 新增生产领料
     *
     * @param productionPicking 生产领料
     * @return 结果
     */
    @Transactional
    @Override
    public int insertProductionPicking(ProductionPicking productionPicking)
    {
        productionPicking.setCreateTime(DateUtils.getNowDate());
        // 获取当前登录用户对象
        LoginUser loginUser = SecurityUtils.getLoginUser();
        //创建人名称
        productionPicking.setCreateBy(loginUser.getUser().getUserName());

        int rows = productionPickingMapper.insertProductionPicking(productionPicking);
        insertProductionPickingItem(productionPicking);
        return rows;
    }

    /**
     * 修改生产领料
     *
     * @param productionPicking 生产领料
     * @return 结果
     */
    @Transactional
    @Override
    public int updateProductionPicking(ProductionPicking productionPicking)
    {
        productionPicking.setUpdateTime(DateUtils.getNowDate());
        // 获取当前登录用户对象
        LoginUser loginUser = SecurityUtils.getLoginUser();
        //修改人名称
        productionPicking.setUpdateBy(loginUser.getUser().getUserName());

        ProductionOrder productionOrder = new ProductionOrder();
        productionOrder.setId(productionPicking.getOrderId());
        productionOrder.setStatus(3l);
        productionOrderMapper.updateProductionOrder(productionOrder);

//        productionPickingMapper.deleteProductionPickingItemByPickingId(productionPicking.getId());
//        insertProductionPickingItem(productionPicking);
        return productionPickingMapper.updateProductionPicking(productionPicking);
    }

    /**
     * 批量删除生产领料
     *
     * @param ids 需要删除的生产领料主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteProductionPickingByIds(String[] ids)
    {
        productionPickingMapper.deleteProductionPickingItemByPickingIds(ids);
        return productionPickingMapper.deleteProductionPickingByIds(ids);
    }

    /**
     * 删除生产领料信息
     *
     * @param id 生产领料主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteProductionPickingById(String id)
    {
        productionPickingMapper.deleteProductionPickingItemByPickingId(id);
        return productionPickingMapper.deleteProductionPickingById(id);
    }


    /**
     * 新增生产领料明细信息
     *
     * @param productionPicking 生产领料对象
     */
    public void insertProductionPickingItem(ProductionPicking productionPicking)
    {

        //修改生产订单状态为进行中
//        ProductionOrder productionOrder = new ProductionOrder();
//        productionOrder.setId(productionPicking.getOrderId());
//        productionOrder.setStatus(1l);
//        productionOrderMapper.updateProductionOrder(productionOrder);


        List<ProductionPickingItem> productionPickingItemList = productionPicking.getProductionPickingItemList();
        String id = productionPicking.getId();
        if (StringUtils.isNotNull(productionPickingItemList))
        {
            List<ProductionPickingItem> list = new ArrayList<ProductionPickingItem>();
            for (ProductionPickingItem productionPickingItem : productionPickingItemList)
            {
                MaterialStock materialStock = new MaterialStock();
                materialStock.setWarehouseId(productionPickingItem.getWarehouseId());
                materialStock.setMaterialId(productionPickingItem.getMaterialId());
                materialStock.setStockQuantity(productionPickingItem.getActualQuantity());
                materialStockMapper.updateMaterialStock(materialStock);


                productionPickingItem.setPickingId(id);

                //随机生成组合商品编号
                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
                String formattedDate = sdf.format(new Date());
                Random random = new Random();
                int randomNumber = 10000000 + random.nextInt(90000000); // 生成8位数随机数
                productionPickingItem.setId("190" + formattedDate+randomNumber);

                list.add(productionPickingItem);
            }
            if (list.size() > 0)
            {
                productionPickingMapper.batchProductionPickingItem(list);
            }
        }
    }
}
